/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

/**
 *
 * @author pc0129
 */
import java.net.*;
import java.io.*;

public class KKMultiServerThread extends Thread {

    private Socket socket = null;

    public KKMultiServerThread(Socket socket) {
        super("KKMultiServerThread");
        this.socket = socket;
    }

    public void run() {

        try {
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    socket.getInputStream()));

            String inputLine, outputLine;
            KnockKnockProtocol kkp = new KnockKnockProtocol();
            outputLine = kkp.processInput(null);
            out.println(outputLine);
            inputLine = in.readLine();
            System.out.println("------------------");
            System.out.println("from client:");
            System.out.println(socket.getInetAddress().toString());
            System.out.println("data:");
            System.out.println(inputLine);
            System.out.println("------------------");

            while (inputLine != null && !inputLine.equalsIgnoreCase("null")) {
                outputLine = kkp.processInput(inputLine);
                out.println(outputLine);
                if (outputLine.equals("STOP")) {
                    break;
                }
            }
            //System.out.print(inputLine);
            out.close();
            in.close();
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}